Vue.component("rebatetype", {
  data() {
    return {
      tabledata: [],
      sval: "",
      selrows: [],
      addVisible: false,
      formLabelWidth: '120px',
      form:{
        tid:0,
        typename:"",
        groupid:'0',
        BL:1
      }
    }
  },
  props: ["path", "col", "showitem", "pval", "vt"],
  template: `<div style="margin:20px;">
      <el-button type="primary" plain @click="addVisible = true">添加</el-button>
      <el-button type="primary" plain @click="delItem()">删除</el-button>
      <el-table :data="tabledata" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column v-for="(item,index) in col" :key="index" :prop="index" :label="item"></el-table-column>
      </el-table>
      <el-dialog title="添加返利类型" :visible.sync="addVisible">
        <el-form :model="form">
          <el-form-item label="类型ID" :label-width="formLabelWidth">
            <el-input v-model="form.tid" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="类型名字" :label-width="formLabelWidth">
            <el-input v-model="form.typename" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="分成类型" :label-width="formLabelWidth">
            <el-radio v-model="form.groupid" label="0">日销量</el-radio>
            <el-radio v-model="form.groupid" label="1">10日净销</el-radio>
          </el-form-item>
          <el-form-item label="分成比率" :label-width="formLabelWidth">
            <el-input v-model="form.BL" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addVisible = false">取 消</el-button>
          <el-button type="primary" @click="addItem()">确 定</el-button>
        </div>
      </el-dialog>
    </div>`,
  methods: {
    opendata: function () { //请求数据
      //console.log("opendata", this, this.path, this.datebe);
      let $this = this;
      if (this.tabledata.view) this.tabledata.view = "";
      http.get("/ajax/" + this.path).then(function (res) {
        $this.addVisible = false;
        $this.tabledata = res.data;
      });
    },
    handleSelectionChange: function(val){ //选择列变化
      this.selrows = val;
    },
    addItem: function () { //添加元素
      let $this = this;
      http.get("/ajax/rebateadd?ti="+this.form.tid+"&tn="+this.form.typename+"&gi="+this.form.groupid+"&bl="+this.form.BL).then(function (res) {
          $this.$message(res.data.m);
          $this.opendata();
      });
    },
    delItem: function(){ //删除元素
      let $this = this;
      let ids = [];
      for(let i=0;i<this.selrows.length;i++) ids.push(this.selrows[i].tid);
      ids = ids.join(",");
      if(ids.length < 1){
        this.$message("请选择数据");
      }
      http.get("/ajax/rebatedel?ids="+ids).then(function (res) {
        $this.$message(res.data.m);
        $this.opendata();
    });
    }
  },
  created: function () {
    console.log("created ", this.path);
    this.opendata();
  }
});